package com.ruoyi.kjcg.ryzz.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.kjcg.ryzz.domain.RyzzFj;
import com.ruoyi.kjcg.ryzz.service.IRyzzService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.kjcg.ryzz.domain.Ryzz;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 荣誉资质Controller
 *
 * @author huacai
 * @date 2024-11-01
 */
@RestController
@RequestMapping("/kjcg/ryzz")
public class RyzzController extends BaseController {
    @Resource
    private IRyzzService ryzzService;

    /**
     * 查询荣誉资质列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:ryzz:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ryzz ryzz) {
        startPage();
        List<Ryzz> list = ryzzService.selectRyzzList(ryzz);
        return getDataTable(list);
    }

    /**
     * 导出荣誉资质列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:ryzz:export')")
    @Log(title = "荣誉资质", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ryzz ryzz) {
        List<Ryzz> list = ryzzService.selectRyzzList(ryzz);
        ExcelUtil<Ryzz> util = new ExcelUtil<Ryzz>(Ryzz.class);
        util.exportExcel(response, list, "荣誉资质数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Ryzz> util = new ExcelUtil<Ryzz>(Ryzz.class);
        util.importTemplateExcel(response, "荣誉资质数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "荣誉资质", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('kjcg:ryzz:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Ryzz> util = new ExcelUtil<Ryzz>(Ryzz.class);
        InputStream inputStream = file.getInputStream();
        List<Ryzz> list = util.importExcel(inputStream);
        inputStream.close();
        int count = ryzzService.batchInsertRyzz(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取荣誉资质详细信息
     */
    @PreAuthorize("@ss.hasPermi('kjcg:ryzz:query')")
    @GetMapping(value = "/{ryzzId}")
    public AjaxResult getInfo(@PathVariable("ryzzId") String ryzzId) {
        return success(ryzzService.selectRyzzByRyzzId(ryzzId));
    }

    /**
     * 新增荣誉资质
     */
    @PreAuthorize("@ss.hasPermi('kjcg:ryzz:add')")
    @Log(title = "荣誉资质", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ryzz ryzz) {
        ryzz.setUserId(Math.toIntExact(getUserId())); //用户ID
        ryzz.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        ryzz.setCreateBy(getUsername()); //创建人
        ryzz.setIdWhere("存在");
        return toAjax(ryzzService.insertRyzz(ryzz));
    }

    /**
     * 修改荣誉资质
     */
    @PreAuthorize("@ss.hasPermi('kjcg:ryzz:edit')")
    @Log(title = "荣誉资质", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ryzz ryzz) {
        return toAjax(ryzzService.updateRyzz(ryzz));
    }

    /**
     * 删除荣誉资质
     */
    @PreAuthorize("@ss.hasPermi('kjcg:ryzz:remove')")
    @Log(title = "荣誉资质", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ryzzIds}")
    public AjaxResult remove(@PathVariable String[] ryzzIds) {
        return toAjax(ryzzService.deleteRyzzByRyzzIds(ryzzIds));
    }

    /**
     * 获得荣誉资质主键ID (UUID)
     */
    @GetMapping("/getRyZzId")
    public AjaxResult getRyZzId(){
        String getRyZzId = IdUtils.fastSimpleUUID();
        return success(getRyZzId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertRyzzFjByRyzzId(@RequestBody RyzzFj ryzzFj){
        return toAjax(ryzzService.insertRyzzFjByRyzzId(ryzzFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectRyzzFjList(String sId){
        RyzzFj ryzzFj = new RyzzFj();
        ryzzFj.setRyzzId(sId);
        return AjaxResult.success(ryzzService.selectRyzzFjList(ryzzFj));
    }

    /**
     * 删除荣誉资质附件
     */
    @Log(title = "荣誉资质附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteRyzzFjByFjIds(@PathVariable Integer[] fjIds)
    {
        return toAjax(ryzzService.deleteRyzzFjByFjIds(fjIds));
    }

}
